#include "StdAfx.h"
#include "TwTextureMgrDx.h"
#include "TwTexture.h"
#include "TwTextureDX.h"

TwTextureMgrDx::TwTextureMgrDx(void):
m_uTexID(0)
{
}

TwTextureMgrDx::~TwTextureMgrDx(void)
{
}
TwTexture* TwTextureMgrDx::CreateTexture(const char* pkPath) 
{
	TwTexture* pkText = FindTeture(pkPath);
	if(pkText != NULL)
		pkText->AddRef();
	else
	{
		pkText = new TwTextureDX();
		if (pkText->CreateTextureFromFile(pkPath))
		{
			pkText->AddRef();
			m_mapTexture[pkPath] = pkText;
			++m_uTexID;
			pkText->SetTexID(m_uTexID);
		}
		else
		{
			delete pkText;pkText = NULL;
		}
	}
	return pkText;
}
TwTexture* TwTextureMgrDx::FindTeture(const char* pkPath) 
{
	if(pkPath == NULL)
		return NULL;
	string str(pkPath);
	if(m_mapTexture.find(str) != m_mapTexture.end())
	{
		return m_mapTexture[str];
	}
	return NULL;
}
const TwTexture* TwTextureMgrDx::GetTexByID(const UINT32 uID)const 
{
	map<string,TwTexture*>::const_iterator itr = m_mapTexture.begin();
	for (; itr != m_mapTexture.end(); ++itr)
	{
		if(itr->second->GetTexID() == uID)
			return itr->second;
	}
	return NULL;
}
void TwTextureMgrDx::Destroy()
{
	map<string,TwTexture*>::const_iterator itr = m_mapTexture.begin();
	for (; itr != m_mapTexture.end(); ++itr)
	{
		itr->second->Destroy();
	}
}